package com.forlove.app.qselect.index.presenter;

import android.text.TextUtils;

import com.forlove.app.ForloveApplication;
import com.forlove.app.common.base.mvp.presenter.IBasePresenter;
import com.forlove.app.common.network.ICallBack;
import com.forlove.app.common.utils.LogUtil;
import com.forlove.app.common.utils.ParseHelper;
import com.forlove.app.manager.DataManager;
import com.forlove.app.manager.NetworkManager;
import com.forlove.app.manager.ServerManager;
import com.forlove.app.manager.UserManager;
import com.forlove.app.qselect.CommonEntity;
import com.forlove.app.qselect.index.entity.FriendCircleInfoEntity;
import com.forlove.app.qselect.index.entity.PageEntity;
import com.forlove.app.qselect.index.service.IFriendCircleInfoService;
import com.forlove.app.qselect.index.service.impl.FriendCircleInfoServiceImpl;
import com.forlove.app.qselect.index.view.FriendCircleInfoView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by yjl on 2017/10/26.
 */

public class FriendCircleInfoPresenter extends IBasePresenter {

    private FriendCircleInfoView friendCircleInfoView;

    private IFriendCircleInfoService friendCircleInfoService;
    private PageEntity pageEntity;
    private List<FriendCircleInfoEntity> infoEntities = new ArrayList<>();

    public FriendCircleInfoPresenter(FriendCircleInfoView friendCircleInfoView) {
        this.friendCircleInfoView = friendCircleInfoView;
        if (friendCircleInfoService == null) {
            friendCircleInfoService = new FriendCircleInfoServiceImpl();
            setService(friendCircleInfoService);
        }
    }

    public synchronized void loadFriendInfoData(final PageEntity pageEntity) {
        if (pageEntity == null) {
            return;
        }
        this.pageEntity = pageEntity;
        Map param = new HashMap();
        param.put("type", pageEntity.mType);
        param.put("page", pageEntity.mPage);
        LogUtil.e("yjl", "page:" + pageEntity.mPage);
        friendCircleInfoService.loadData(ServerManager.ACTION_GET_INDEX_INFO, param, new ICallBack() {
            @Override
            public void onSuccess(int statusCode, String result) {
                LogUtil.e("yjl", "好友信息：" + result);
                pageEntity.mPage = ParseHelper.getInteger(result, "data.page");
                List<FriendCircleInfoEntity> friendCircleInfoEntities =
                        ParseHelper.getModelList(result, "data.list", FriendCircleInfoEntity.class);
                if (pageEntity.mPage == 1 && friendCircleInfoEntities != null) {
                    infoEntities.clear();
                    infoEntities.addAll(friendCircleInfoEntities);
                    //保存数据记录
                    DataManager.saveFriendCircleInfo(ForloveApplication.getInstance(), result);
                    if (friendCircleInfoEntities.size() > 0) {
                        FriendCircleInfoEntity friendCircleInfoEntity = friendCircleInfoEntities.get(0);
                        UserManager.saveNewCircleInfo(ForloveApplication.getInstance(), friendCircleInfoEntity.recordId);
                    }
                } else if (pageEntity.mPage > 1
                        && friendCircleInfoEntities != null
                        && friendCircleInfoEntities.size() > 0) {
                    infoEntities.addAll(friendCircleInfoEntities);
                } else if (friendCircleInfoEntities == null || friendCircleInfoEntities.size() == 0) {
                    if (pageEntity.mPage > 1) {
                        pageEntity.mPage--;
                    }
                }
                if (friendCircleInfoView != null) {
                    friendCircleInfoView.updateFriendCircleInfoView(pageEntity, infoEntities);
                }
            }

            @Override
            public void onFail(int statusCode, String result) {
                CommonEntity commonEntity = new CommonEntity();
                commonEntity.parseJSONString(result);
                if (!TextUtils.isEmpty(commonEntity.message)) {
                    if (friendCircleInfoView != null) {
                        friendCircleInfoView.showErrorData(NetworkManager.ResultCode.RESULT_FAIL, commonEntity.message);
                    }
                } else {
                    if (friendCircleInfoView != null) {
                        friendCircleInfoView.showErrorData(NetworkManager.ResultCode.RESULT_ERROR, "稍后重试!");
                    }
                }
            }
        });

    }

    public synchronized void loadMoreFriendInfData() {
        if (this.pageEntity != null) {
            pageEntity.mPage++;
        }
//        LogUtil.e("yjl", "loadMoreData:" + pageEntity.mPage);
        loadFriendInfoData(pageEntity);
    }

}
